package com.samp.base.list;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ListCompare {
	
	@SuppressWarnings("unchecked")
	public static List<String>[] findDiff(List<String> previousList, List<String> newList){
		List<String> addList = null;
		List<String> deleteList = null;
		if( previousList == null || previousList.size() == 0 ){
			//全部为新增。
			addList = newList;
		}else if( newList == null || newList.size() == 0 ){
			//全部为删除
			deleteList = previousList;
		}else{
			List<String> tmpPrevious = new ArrayList<String>(Arrays.asList(new String[previousList.size()]));
			List<String> tmpNew = new ArrayList<String>(Arrays.asList(new String[newList.size()]));
			Collections.copy(tmpPrevious, previousList);
			Collections.copy(tmpNew, newList);
			for( int i = tmpNew.size() -1 ; i >=0 ; i-- ){
				String word = tmpNew.get(i);
				if( tmpPrevious.contains(word) ){
					tmpNew.remove(i);
					tmpPrevious.remove(word);
				}
			}
			addList = tmpNew;
			deleteList = tmpPrevious;
		}
		return new List[]{addList, deleteList};
	}
	
	public static void main(String[] args) {
		//List<String> previousList = Arrays.asList("4","5","6","7","8");
		//List<String> newList = Arrays.asList("1","2","3","4","5","6","7","8");
		List<String> previousList = new ArrayList<String>();
		previousList.add("1");
		previousList.add("2");
		previousList.add("3");
		List<String> newList = new ArrayList<String>();
		newList.add("2");
		newList.add("3");
		List<String>[] dd = findDiff(previousList, newList);
		System.out.println("addList");
		System.out.println(dd[0]);
		System.out.println("deleteList");
		System.out.println(dd[1]);
		System.out.println("previousList");
		System.out.println(previousList);
		System.out.println("newList");
		System.out.println(newList);
	}
}
